package org.judo.util;

import java.lang.reflect.Field;
import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import org.judo.service.systemservices.Logger;

/**
 *This class contains a number of convenience methods that
 *are available to any class that inherits from this one.
 */
public class ConvenienceBase
{
	/**
	 * If any subclasses have any fields that implement
	 * org.judo.service.systemservices.Logger then this method
	 * will use that to output the error message.  Otherwise
	 * it will print out the message to System.out 
	 */
	public void info(Object msg)
	{
		Logger log = findFirstLogger();
		
		if (log == null)
			System.out.println("INFO: ("+this.getClass().getSimpleName()+"): "+msg);
		else
			log.info(this,msg);
	}
	
	/**
	 * If any subclasses have any fields that implement
	 * org.judo.service.systemservices.Logger then this method
	 * will use that to output the error message.  Otherwise
	 * it will print out the message to System.out 
	 */
	public void debug(Object msg)
	{
		Logger log = findFirstLogger();
		
		if (log == null)
			System.out.println("DEBUG: ("+this.getClass().getSimpleName()+"): "+msg);
		else
			log.debug(this,msg);
	}
	
	/**
	 * If any subclasses have any fields that implement
	 * org.judo.service.systemservices.Logger then this method
	 * will use that to output the error message.  Otherwise
	 * it will print out the message to System.err 
	 */
	public void warn(Object msg)
	{
		Logger log = findFirstLogger();
		
		if (log == null)
			System.out.println("WARN: ("+this.getClass().getSimpleName()+"): "+msg);
		else
			log.warn(this,msg);
	}
	
	/**
	 * If any subclasses have any fields that implement
	 * org.judo.service.systemservices.Logger then this method
	 * will use that to output the error message.  Otherwise
	 * it will print out the message to System.err 
	 */
	public void error(Object msg)
	{
		Logger log = findFirstLogger();
		
		if (log == null)
			System.out.println("ERROR: ("+this.getClass().getSimpleName()+"): "+msg);
		else
			log.error(this,msg);
	}
	
	/**
	 * If any subclasses have any fields that implement
	 * org.judo.service.systemservices.Logger then this method
	 * will use that to output the error message.  Otherwise
	 * it will print out the message to System.err 
	 */
	public void fatal(Object msg)
	{
		Logger log = findFirstLogger();
		
		if (log == null)
			System.out.println("FATAL: ("+this.getClass().getSimpleName()+"): "+msg);
		else
			log.fatal(this,msg);
	}
	
	public boolean isNullOrEmpty(String s)
	{
		if (s == null)
			return true;
		
		if (s.equals(""))
			return true;
		
		return false;
	}
	
	public boolean isUnsellected(String s)
	{
		if (s == null)
			return true;
		
		if (s.equals(""))
			return true;
		
		if (s.equals("NA"))
			return true;
		
		return false;
	}
	
	public void dump()
	{
	}
	
	
	public Date toDate(String date)
	{
		return null;
	}
	
	public Timestamp toTimetamp(String time)
	{
		return null;
	}
	
	protected Logger findFirstLogger()
	{
		try
		{
			Field fields[] = this.getClass().getDeclaredFields();
			
			for (Field field: fields)
			{
				field.setAccessible(true);
				if (Logger.class.isAssignableFrom(field.getType()))
					return (Logger)field.get(this);
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		
		return null;
	}
}
